<template>
	<view class="applyFor-index">
		<u-notice-bar v-if="noticeShow" @close="noticeShow=false" mode="horizontal" :list="notice" :close-icon="true" color="#1F1F1F" volume-color="#2878ff" bg-color="#EBF3FF" padding="24rpx 24rpx"></u-notice-bar>
		<view class="applyFor-index-content">
			<view class="flex-between-center color-fff">
				<view class="">借款金额</view>
				<view class="flex-align-center" @click="$common.navigator('/pagesA/quota/index')">
					<view class="">去提额</view>
					<static-image src="/static/img/common/arrow-right-white.png" width="24" height="24" margin-left="4"></static-image>
				</view>
			</view>
			<view class="apply-money-box">
				<text class="unit-text">￥</text>
				<view class="mar-l-5 flex-1">
					<u-input :disabled="true" :value="form.applyAmount" placeholder="暂无可用额度" :custom-style="{color:'#fff',fontSize:'88rpx',fontWeight:'bold',letterSpacing: '8rpx'}" placeholder-style="color:#eee;font-size:70rpx;fontWeight:initial;letter-spacing: 0px;"></u-input>
				</view>
			</view>
			<view class="form-card-box">
				<view class="flex-between-center">
					<view class="fw-b">借多久</view>
					<fu-picker v-model="form.applyTotalDay" :options="dayOptions"></fu-picker>
				</view>
				<view class="flex-between-center">
					<view class="fw-b">几期还</view>
					<fu-picker v-model="form.applyTotalTerm" :options="termOptions"></fu-picker>
				</view>
				<view class="flex-between-center">
					<view class="fw-b">怎么还</view>
					<fu-picker v-model="form.repayMethod" :options="methodOptions"></fu-picker>
				</view>
				<view class="flex-between-center">
					<view class="fw-b">借款用途</view>
					<fu-picker v-model="form.loanPurpose" :options="purposeOptions"></fu-picker>
				</view>
			</view>
			<view class="other-info">
				<view class="flex-between-center">
					<view class="flex-align-center">
						<static-image src="/pagesA/static/img/applyFor/icon-hkjh.png" width="48" height="48" margin-right="16"></static-image>
						<view class="fw-b">还款计划</view>
						<view class="status-tag wait-tag" v-if="btnDisabled">待生成</view>
						<view class="status-tag" v-else>已生成</view>
					</view>
					<view class="flex-align-center" v-if="!btnDisabled" @click="handlePlan()">
						<view class="color-999">去查看</view>
						<static-image src="/static/img/common/arrow-right.png" width="24" height="24" margin-left="4"></static-image>
					</view>
				</view>
				<view class="flex-between-center">
					<view class="flex-align-center">
						<static-image src="/pagesA/static/img/applyFor/icon-skzh.png" width="48" height="48" margin-right="16"></static-image>
						<view class="fw-b">银行绑卡</view>
						<view class="status-tag" v-if="form.secondFourAuthentication===1">已绑定</view>
						<view class="status-tag wait-tag" v-else>待绑定</view>
					</view>
					<view class="flex-align-center" @click="$common.navigator(form.secondFourAuthentication===1?'/pagesA/applyFor/bankCard':'/pagesA/applyFor/changeBankCard')">
						<view class="color-999">{{form.secondFourAuthentication===1?'去查看':'去添加'}}</view>
						<static-image src="/static/img/common/arrow-right.png" width="24" height="24" margin-left="4"></static-image>
					</view>
				</view>
				<view class="flex-between-center">
					<view class="flex-align-center">
						<static-image src="/pagesA/static/img/applyFor/icon-wszl.png" width="48" height="48" margin-right="16"></static-image>
						<view class="fw-b">完善资料</view>
						<view class="status-tag" v-if="form.basicIsFinish===1">已完善</view>
						<view class="status-tag wait-tag" v-else>待完善</view>
					</view>
					<view class="flex-align-center" @click="$common.navigator('/pagesA/applyFor/improveInfo')">
						<view class="color-999">{{form.basicIsFinish===1?'去查看':'去完善'}}</view>
						<static-image src="/static/img/common/arrow-right.png" width="24" height="24" margin-left="4"></static-image>
					</view>
				</view>
			</view>
		</view>
		<footer-btn>
			<view class="agreement-box" @click="agreement=!agreement">
				<static-image v-if="agreement" src="/static/img/common/agreement-active.png" width="32" height="32"></static-image>
				<static-image v-else src="/static/img/common/agreement-no.png" width="32" height="32"></static-image>
				<view class="mar-l-5">
					<text>我已阅读并同意</text>
					<text class="color-blue" @click.stop="$common.navigator('/pagesA/setting/applyAgreement')">《用户申请协议》</text>
				</view>
			</view>
			<u-button :disabled="btnDisabled" shape="circle" @click="confirm()" type="primary" :custom-style="{background:'#1F78FF',fontSize:'32rpx',fontWeight:'bold',height:'90rpx',letterSpacing: '8rpx'}">{{form.id?'确认修改':'确认提交'}}</u-button>
		</footer-btn>
		<!-- 还款计划 -->
		<u-popup mode="bottom" v-model="planShow" border-radius="24" :closeable="true">
			<view class="popup-title">还款计划</view>
			<view class="plan-total">
				<view class="font-s-24 fw-b">{{planInfo.totalRepayAmount || 0}}</view>
				<view class="font-s-12 color-999 mar-t-5">总借款金额 (元)</view>
				<view class="plan-total-border flex-between-center">
					<view class="">
						<text class="color-666">本金：</text>
						<text class="fw-b">￥{{planInfo.applyAmount || 0}}</text>
					</view>
					<view class="">
						<text class="color-666">利息：</text>
						<text class="fw-b">￥{{planInfo.totallixi || 0}}</text>
					</view>
					<view class="">
						<text class="color-666">担保费：</text>
						<text class="fw-b">￥{{planInfo.totalAssureAmount || 0}}</text>
					</view>
				</view>
			</view>
			<scroll-view scroll-y="true" class="plan-scroll" v-if="planInfo.list && planInfo.list.length">
				<view class="plan-scroll-content">
					<view class="plan-item" v-for="(item,index) in planInfo.list" :key="index">
						<view style="margin-top: 2rpx;">
							<view class="font-s-16 fw-b">{{index?'第'+(index+1):'首'}}期</view>
							<view class="color-999 font-s-12 mar-t-8">{{item.planEndDate}}</view>
						</view>
						<view class="flex-1">
							<view class="font-s-16 fw-b color-red">{{item.remainShouldTotalAmt || 0}}元</view>
							<view class="other-money">本金{{item.remainShouldPrincipal || 0}}元，利息{{item.remainShouldInterest || 0}}元</view>
							<view class="other-money">担保费{{item.remainShouldAssure || 0}}元</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<empty text="暂无还款计划" height="80vh" v-else></empty>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				agreement: false,
				notice:[],
				noticeShow: true,
				purposeOptions:[],
				methodOptions:[],
				termOptions:[],
				dayOptions:[],
				form:{
					applyAmount: 0,
					applyTotalDay: '',
					applyTotalTerm: '',
					repayMethod: '',
					loanPurpose: '',
					fourAuthentication:0,
					secondFourAuthentication:0,
					basicIsFinish:0
				},
				planInfo: {
					list: []
				},
				planShow: false,
			}
		},
		computed:{
			btnDisabled(){
				return !this.form.applyAmount || !this.form.applyTotalDay || !this.form.applyTotalTerm || !this.form.repayMethod || !this.form.loanPurpose
			}
		},
		onLoad(e) {
			this.orderId = e.orderId
			if(this.orderId){
				this.getOrderInfo()
			}
			this.getConfigKey()
			this.getDicts()
		},
		onShow() {
			this.handleQuery()
		},
		methods: {
			handleQuery(){
				this.$request({
					url: '/loginUserInfo/getDetail',
					method: 'get'
				}).then(res => {
					this.form.applyAmount = res.data.availableAmount
					this.form.fourAuthentication = res.data.fourAuthentication
					this.form.secondFourAuthentication = res.data.secondFourAuthentication
					this.form.basicIsFinish = res.data.basicIsFinish
				})
			},
			getOrderInfo(){
				this.$request({
					url: '/orderInfo/getDetail',
					method: 'get',
					data:{id:this.orderId}
				}).then(res => {
					this.form = res.data
				})
			},
			getConfigKey(){
				this.$request({
					url: '/system/config/configKey/apply_notice',
					method: 'get'
				}).then(res => {
					this.notice = [res.msg]
				})
			},
			getDicts(){
				this.$request({
					url: '/system/dict/data/type/loan_purpose',
					method: 'get'
				}).then(res => {
					this.purposeOptions = res.data;
				})
				this.$request({
					url: '/system/dict/data/type/repayment_method',
					method: 'get'
				}).then(res => {
					this.methodOptions = res.data;
				})
				this.$request({
					url: '/system/dict/data/type/repayment_term',
					method: 'get'
				}).then(res => {
					this.termOptions = res.data;
				})
				this.$request({
					url: '/system/dict/data/type/loan_days',
					method: 'get'
				}).then(res => {
					this.dayOptions = res.data;
				})
			},
			handlePlan(){
				this.$request({
					url: '/orderInfo/planBill',
					method: 'post',
					data: this.form
				}).then(res => {
					this.planInfo = res.data;
					this.planShow = true
				})
			},
			confirm(){
				if(this.form.secondFourAuthentication!==1){
					return this.$u.toast('请先绑定银行卡');
				}
				if(this.form.basicIsFinish===0){
					return this.$u.toast('请先完善资料');
				}
				if(!this.agreement){
					return this.$u.toast('请先阅读并同意《用户服务协议》');
				}
				uni.showLoading({ mask:true })
				this.$request({
					url: '/orderInfo/save',
					method: 'post',
					data: this.form
				}).then(res => {
					this.$u.toast('提交成功');
					this.form.id = res.data.id
					uni.redirectTo({
						url: '/pagesA/applyFor/success?orderId='+this.form.id
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.applyFor-index {
		&-content{
			background-image: url('../static/img/applyFor/header-bg.png');
			background-size: 100% 504rpx;
			background-repeat: no-repeat;
			padding: 48rpx 24rpx;
		}
		.apply-money-box{
			border-bottom: 2rpx solid rgba(255,255,255,0.24);
			color: #fff;
			padding: 24rpx 0 16rpx;
			display: flex;
			.unit-text{
				font-size: 56rpx;
				font-weight: bold;
				margin-top: 36rpx;
			}
		}
		.form-card-box{
			background: #FFFFFF;
			border-radius: 24rpx;
			margin-top: 48rpx;
			padding: 0 24rpx;
			>view{
				height: 124rpx;
				&:not(:last-child){
					border-bottom: 2rpx solid #F5F5F5;
				}
			}
		}
		.other-info{
			>view{
				height: 124rpx;
				background: #FFFFFF;
				border-radius: 24rpx;
				margin-top: 24rpx;
				padding: 0 24rpx;
				.status-tag{
					width: 80rpx;
					height: 32rpx;
					line-height: 32rpx;
					background: rgba(24,204,132,0.08);
					border-radius: 4rpx;
					font-size: 24rpx;
					color: #18CC84;
					text-align: center;
					margin-left: 8rpx;
					&.wait-tag{
						background: rgba(255,133,51,0.08);
						color: #FF8533;
					}
				}
			}
		}
		.plan-total{
			width: 100%;
			padding: 24rpx 48rpx 32rpx;
			background: #FAFBFC;
			text-align: center;
			.plan-total-border{
				font-size: 24rpx;
				border-top: 2rpx solid #F0F0F0;
				padding-top: 32rpx;
				margin-top: 24rpx;
			}
		}
		.plan-scroll{
			height: 600rpx;
			.plan-scroll-content{
				padding: 24rpx 24rpx 0;
				.plan-item{
					display: flex;
					padding-bottom: 48rpx;
					position: relative;
					>view:first-child{
						width: 260rpx;
					}
					.other-money{
						width: 100%;
						height: 56rpx;
						line-height: 56rpx;
						background: #FAFBFC;
						border-radius: 8rpx;
						font-size: 24rpx;
						margin-top: 16rpx;
						padding: 0 16rpx;
					}
					&::before{
						content: '';
						width: 14rpx;
						height: 14rpx;
						background: #1F78FF;
						border-radius: 50%;
						border: 6rpx solid #E6F5FA;
						position: absolute;
						left: 188rpx;
						top: 8rpx;
					}
					&:not(:last-child)::after{
						content: '';
						width: 2rpx;
						height: calc(100% - 24rpx);
						background: #F0F4FA;
						position: absolute;
						left: 200rpx;
						top: 32rpx;
					}
				}
			}
		}
		.agreement-btn{
			box-shadow: 0rpx -2rpx 0rpx 0rpx #F5F5F5;
			padding: 16rpx 24rpx;
		}
		.agreement-box{
			display: flex;
			margin-bottom: 24rpx;
			padding-top: 8rpx;
		}
	}
</style>
